<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 地址接口
 */
class Address extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 我的地址
     * @param string $page 页码，第几页，默认为1
     * @param string $page_size 每页数量，默认为15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturnParams   (name="name", type="object", description="收货人姓名")
     * @ApiReturnParams   (name="mobile", type="object", description="手机号")
     * @ApiReturnParams   (name="area", type="object", description="地址")
     * @ApiReturnParams   (name="type", type="object", description="类型:1=默认,2=非默认")
     * @ApiReturnParams   (name="address", type="object", description="详细地址")
     *
     */
    public function getUserAddressList()
    {
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        $token = $this->auth->getToken();
        $user = \app\common\library\Token::get($token);
        
        $where = array(
            "user_id" => $user['user_id']
        );
        $result = DB::name("user_address")->where($where)->order("id desc")->page($page,$page_size)->select();
        
        $this->success('请求成功',$result);
    }
    
    /**
     * 添加地址
     * @ApiMethod (POST)
     * @param string $name 姓名
     * @param string $mobile 手机号
     * @param string $area 地址
     * @param string $address 用户地址
     * @param string $type 类型:1=默认,2=非默认
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     *
     */
    public function addUserAddress()
    {
        $name = input("name")?input("name"):$this->error("收货人姓名不能为空");
        $mobile = input("mobile")?input("mobile"):$this->error("手机号不能为空");
        $area = input("area")?input("area"):$this->error("地址不能为空");
        $address = input("address")?input("address"):$this->error("详细地址不能为空");
		$type = input("type")?input("type"):1;
        
        $token = $this->auth->getToken();
        $user = \app\common\library\Token::get($token);
        
		$user_address = DB::name("user_address")->where("user_id",$user['user_id'])->find();
		if($user_address){
			if($type == 1){
				DB::name("user_address")->where("user_id",$user['user_id'])->update(array("type"=>2));
			}
		}else{
			$type = 1;
		}
		
        $data = array(
            "user_id" => $user['user_id'],
            "name" => $name,
            "mobile" => $mobile,
            "area" => $area,
            "address" => $address,
			"type" => $type,
            "create_time" => date("Y-m-d H:i:s")
        );
        $user_address_id = DB::name("user_address")->insertGetId($data);
        if($user_address_id){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
        
    }
    
    
    /**
     * 修改地址
     * @ApiMethod (POST)
     * @param string $user_address_id 地址id
     * @param string $user_name 姓名
     * @param string $mobile 手机号
     * @param string $area 地址
     * @param string $address 详细地址
     * @param string $type 类型:1=默认，2=非默认
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     *
     */
    public function editUserAddress()
    {
        $user_address_id = input("user_address_id")?input("user_address_id"):$this->error("地址id不能为空");
        $name = input("user_name")?input("user_name"):$this->error("姓名不能为空");
        $mobile = input("mobile")?input("mobile"):$this->error("手机号不能为空");
        $area = input("area")?input("area"):$this->error("地区不能为空");
        $address = input("address")?input("address"):$this->error("详细地址不能为空");
		$type = input("type")?input("type"):1;
        
        $token = $this->auth->getToken();
        $user = \app\common\library\Token::get($token);
        
        $user_address = DB::name("user_address")->where("user_id =".$user['user_id']." and id !=".$user_address_id)->find();
		if($user_address){
			if($type == 1){
				DB::name("user_address")->where("user_id",$user['user_id'])->update(array("type"=>2));
			}else{
				$other_user_address = DB::name("user_address")->where("id",$user_address_id)->find();
				if($other_user_address['type'] == 1){
					$type = 1;
				}
			}
		}else{
			$type = 1;
		}
		
        $data = array(
            "user_id" => $user['user_id'],
            "name" => $name,
            "mobile" => $mobile,
            "area" => $area,
            "address" => $address,
			"type" => $type,
        );
        
        $user_address_id = DB::name("user_address")->where("id",$user_address_id)->update($data);
        if($user_address_id){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
        
    }
    
     /**
     * 删除地址
     * @ApiMethod (POST)
     * @param string $user_address_id 地址id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     *
     */
    public function delUserAddress()
    {
        $user_address_id = input("user_address_id")?input("user_address_id"):$this->error("地址id不能为空");
        
        $token = $this->auth->getToken();
        $user = \app\common\library\Token::get($token);
        
        $user_address_data = array(
            "user_id" => $user['user_id'],
            "id" => $user_address_id,
        );
        
        $user_address_find = DB::name("user_address")->where($user_address_data)->find();
        if(empty($user_address_find)){
            $this->error("地址不存在");
        }
		if($user_address_find['type'] == 1){
			$other_address = DB::name("user_address")->where("user_id =".$user['user_id']." and id != ".$user_address_id)->find();
			if($other_address){
				DB::name("user_address")->where("id",$other_address['id'])->update(array("type"=>1));
			}
		}
        $result = DB::name("user_address")->where("id",$user_address_id)->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
        
    }
}
